package com.inifitness.admin.core.system.controller;


import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inifitness.admin.core.system.model.form.EventParamForm;
import com.inifitness.admin.core.system.model.query.EventParamPageQuery;
import com.inifitness.admin.core.system.model.vo.EventParamPageVO;
import com.inifitness.admin.core.system.service.SysEventParamService;
import com.inifitness.common.annotations.PreventDuplicateResubmit;
import com.inifitness.common.model.Option;
import com.inifitness.common.result.PageResult;
import com.inifitness.common.result.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统管理-事件参数服务服务实现类
 *
 * @author
 * @since 2024-05-31 13:46:16
 */

@Tag(name = "系统管理-事件参数")
@Log4j2
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/manger/v1/")
public class SysEventParamController {

  /**
   * 服务对象
   */
  private final SysEventParamService sysEventParamService;


  @Operation(summary = "分页列表")
  @GetMapping("/eventParam/page")
  public PageResult<EventParamPageVO> getPage(@ParameterObject EventParamPageQuery queryParams) {
    Page<EventParamPageVO> result = sysEventParamService.getEventParamPage(queryParams);
    return PageResult.success(result);
  }


  @Operation(summary = "下拉列表", security = {@SecurityRequirement(name = "Authorization")})
  @GetMapping("/eventParam/options")
  public Result<List<Option<Long>>> listEventParamOptions(
      @Parameter(description = "事件编码") @PathVariable(value = "eventCode") String eventCode) {
    List<Option<Long>> options = sysEventParamService.listEventParamOptions(eventCode);
    return Result.success(options);
  }

  @Operation(summary = "对象表单数据")
  @GetMapping("/eventParam/{id}/form")
  public Result<EventParamForm> getEventParamForm(
      @Parameter(description = "ID") @PathVariable(value = "id") Long id) {
    EventParamForm form = sysEventParamService.getEventParamForm(id);
    return Result.success(form);
  }

  @Operation(summary = "新增对象")
  @PostMapping("/eventParam")
  @PreAuthorize("@ss.hasPerm('sys:event:add')")
  @PreventDuplicateResubmit
  public Result<Void> saveEventParam(@RequestBody EventParamForm form) {
    boolean result = sysEventParamService.saveEventParam(form);
    return Result.judge(result);
  }

  @Operation(summary = "修改对象")
  @PutMapping("/eventParam/{id}")
  @PreAuthorize("@ss.hasPerm('sys:event:edit')")
  public Result<Void> updateEventParam(@PathVariable(value = "id") Long id,
      @RequestBody EventParamForm form) {
    boolean status = sysEventParamService.updateEventParam(id, form);
    return Result.judge(status);
  }

  @Operation(summary = "删除对象")
  @DeleteMapping("/eventParam/{ids}")
  @PreAuthorize("@ss.hasPerm('sys:event:delete')")
  public Result<Void> deleteEventParams(
      @Parameter(description = "对象ID，多个以英文逗号(,)分割") @PathVariable(value = "ids") String ids) {
    boolean result = sysEventParamService.deleteEventParams(ids);
    return Result.judge(result);
  }
}
